<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller {
  
  public function _initialize() {
		header("Content-Type:text/html; charset=utf-8");
       
		//echo $czmc;die;
	
		if (! isset ( $_SESSION ['adminuser'] )) {
			// $this->error('請先登錄!',U('Login/index'));
			
			$this->success('請先登錄','/Admin/Home/Login');die;
		}
		$Message =  M('message');
		$count = $Message->where(array('status'=>0))->count();
		$Setting =  M('setting');
		$setting = $Setting->where(array('id'=>1))->find();
		define('ZHITUI',$setting['zhitui']/100);
		define('COUNTS',$count);

		

		$last=M('reward')->where(array('typemoney' => 1))->order('times_c desc')->limit(1)->find();
        if(date('Y-m-d', $last['times_c'])!=date('Y-m-d', time())) {
            // echo date('Y-m-d', $last['createtime']);
            $quanbu=M('account')->where('status=1')->select();
            // dump($quanbu);die;

            for($j=0;$j<count($quanbu);$j++){
                $this->backcash($quanbu[$j]['id']);//每天返现收益(日分红)
                $this->jiangjin($quanbu[$j]['id']);//管理奖
            }

        }
		
	}


	private function jiangjin($userid)
    {
        // $userid = session('userid');
        $map['id'] = $userid;
        $map['status'] = 1;//0为未激活 1为已激活
        // $map['level'] = 1;//1为代理
        $map['level'] = array('gt',1);//为代理

        $daili_user=M('account')->where($map)->find();

        if(empty($daili_user)){

            return;

        }

        $dailitim=$daili_user['daitime'];
        $dailiday=$daili_user['days2'];

        if(date('Y-m-d', $dailitim)!=date('Y-m-d', time())) {
            $day1 = date('Y-m-d', $dailitim + 24 * 60 * 60);
            $day2 = date('Y-m-d', time());
            $diff = $this->diffBetweenTwoDays($day1, $day2);
            $diff = $diff - $dailiday;
            for ($i = 0; $i <= $diff; $i++) {

                //设置十二点返现
                $createtime = $dailitim + ($dailiday + $i) * 24 * 60 * 60;
                $createtime = date('Y-m-d', $createtime + 24 * 60 * 60);
                $createtime = strtotime($createtime);

                $this->guanli($userid,$createtime);

                //增加返现天数
                $arr = array(
                    'days2' => array('exp', 'days2+1'),
                );
        
                M('account')->where('id='.$userid)->save($arr);

            }
        }

    }

    //管理奖
    private function guanli($userid,$createtime)
    {
        $map['id'] = $userid;
        $map['status'] = 1;//0为未激活 1为已激活
        // $map['level'] = 1;//1为代理
        $map['level'] = array('gt',1);

        $daili=M('account')->where($map)->find();      

        if(empty($daili)){

            return;

        }

        $z_par = M('account')->where(array('parid' => $daili['id'],'status' => 1))->select();
        $z_parnum = count($z_par);

        if($z_parnum==0){
                
            return;

        }

        $sqlw='1=1 and status=1 and';

        for($i=0;$i<$z_parnum;$i++)
        {
            if($i==0){
                $sqlw.=' (';
            }
            $id1 = $z_par[$i]['id'];
            $sqlw.=' parid='.$id1;
            if($i<$z_parnum-1){
                $sqlw.=' or ';
            }else{
                $sqlw.=') ';
            }

        }
        
        // if($daili['investmoney']<5000){
        if($daili['level']==2){

            if($z_parnum>1){

                if($z_parnum>9){

                    $daishu=9;

                }else{
                    $daishu=$z_parnum;
                }

            }else{

                return;

            }


        // }elseif($daili['investmoney']>=5000 && $daili['investmoney']<10000){
        }elseif($daili['level']==3){
    
            if($z_parnum>3 && $z_parnum<=9){

                $daishu=$z_parnum;

            }
            if($z_parnum>9){
                
                $daishu=9;

            }
            if($z_parnum<=3){
                
                $daishu=3;

            }


        // }elseif($daili['investmoney']>=10000 && $daili['investmoney']<20000){
        }elseif($daili['level']==4){
            
            if($z_parnum>5 && $z_parnum<=9){

                $daishu=$z_parnum;

            }
            if($z_parnum>9){
                
                $daishu=9;

            }
            if($z_parnum<=5){
                
                $daishu=5;

            }
            

        // }elseif($daili['investmoney']>=20000 && $daili['investmoney']<30000){
        }elseif($daili['level']==5){
            
            $daishu=9;

        }
        // elseif($daili['investmoney']>=30000){
            
        //     $daishu=9;

        // }

        $setting=M('setting')->where('id=1')->find();

        for($a=1;$a<$daishu;$a++)
        {
            if($a==1 || $a==2){
                $bfb=0.09;
            }
            if($a==3 || $a==4){
                 $bfb=0.07;
            }
            if($a==5 || $a==6){
                $bfb=0.05;
            }
            if($a==7 || $a==8){
                $bfb=0.03;
            }
            // $z_bfb=$bfb * 0.008;
            $z_bfb=$bfb * $setting['rate']/100;

            $sqlw=$this->next_user($sqlw,$a,$z_bfb,$createtime,$userid);
            if(empty($sqlw)){
                 break;
            }

        }

    }
    
    //下一代
    private function next_user($sqlw,$a,$z_bfb,$createtime,$userid)
    {
        $dainum=$a+1;
        $z_par=M('account')->where($sqlw)->select();
        $sqlw2='1=1 and status=1 and';
        $z_parnum = count($z_par);
        
        if($z_parnum==0){

            return;

        }

        for($j=0;$j<$z_parnum;$j++){

            $price = $z_par[$j]['investmoney']*$z_bfb;
            $title='第'.$dainum.'代会员'.$z_par[$j]['account'].'的管理奖';

            $this->doincomefrom($userid,$title,$price,$createtime,3);//每天生成管理奖记录

            if($j==0){
                $sqlw2.=' (';
            }

            $id1 = $z_par[$j]['id'];
            $sqlw2.=' parid='.$id1;
            if($j<$z_parnum-1){
                $sqlw2.=' or ';
            }else{
                $sqlw2.=') ';
            }

        }

        return $sqlw2;
    }

    //每天返现收益
    private function backcash($userid)
    {
        // $userid = session('userid');
        $map['id'] = $userid;
        $map['status'] = 1;

        $jh_user=M('account')->where($map)->find();
        $setting=M('setting')->where('id=1')->find();

        if(date('Y-m-d', $jh_user['jhtime'])!=date('Y-m-d', time())) {
            $day1 = date('Y-m-d', $jh_user['jhtime'] + 24 * 60 * 60);
            $day2 = date('Y-m-d', time());
            $diff = $this->diffBetweenTwoDays($day1, $day2);
            $diff = $diff - $jh_user['days'];
            $title = '投资额日分红';
            for ($i = 0; $i <= $diff; $i++) {
                // $price = $jh_user['investmoney'] * 0.008;//每天返现
                if($jh_user['investmoney']!=0){

                    $price = $jh_user['investmoney'] * $setting['rate']/100;//每天返现

                }
                // $price1 = $price * 0.5;//每天返现50%进提现钱包
                // $price2 = $price * 0.2;//每天返现20%进季度钱包
                // $price3 = $price * 0.3;//每天返现30%进半年钱包
                //1写入收益

                //设置十二点返现
                $createtime = $jh_user['jhtime'] + ($jh_user['days'] + $i) * 24 * 60 * 60;
                $createtime = date('Y-m-d', $createtime + 24 * 60 * 60);
                $createtime = strtotime($createtime);

                if($price){

                    $this->doincomefrom($userid, $title, $price, $createtime, 1);//每天返现记录

                }

                //2减去返现
                $arr = array(
                    'days' => array('exp', 'days+1'),
                    // 'cashmoney' => array('exp', 'cashmoney+' . $price1),
                    // 'halfmoney' => array('exp', 'halfmoney+' . $price2),
                    // 'yearmoney' => array('exp', 'yearmoney+' . $price3),
                );
                
                M('account')->where($map)->save($arr);

            }
        }
        
    }

    private function doincomefrom($userid,$title,$price,$createtime,$status)
    {
        $D=M('reward');
        $arr=array(
            'uid'=>$userid,
            'title'=>$title,
            'money'=>$price,
            'times_c'=>$createtime,
            'typemoney'=>$status,
        );
        $D->add($arr);

        $price1 = $price * 0.5;//每天返现50%进提现钱包
        $price2 = $price * 0.2;//每天返现20%进季度钱包
        $price3 = $price * 0.3;//每天返现30%进半年钱包

        $arr2 = array(
            'cashmoney' => array('exp', 'cashmoney+' . $price1),
            'halfmoney' => array('exp', 'halfmoney+' . $price2),
            'yearmoney' => array('exp', 'yearmoney+' . $price3),
        );
        
        M('account')->where('id='.$userid)->save($arr2);

    }

    /**
     * 求两个日期之间相差的天数
     * (针对1970年1月1日之后，求之前可以采用泰勒公式)
     * @param string $day1
     * @param string $day2
     * @return number
     */
    private function diffBetweenTwoDays ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }




}